<!-- 课程管理(模块) => 课程列表(菜单) => 台账操作列(学生列表按钮) => 点开后展示此组件 -->
<template>
  <Dialog :title="t('studentCourse.list.title')" width="1400" v-model="dialogVisible">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('studentCourse.list.studentName')" prop="studentName">
          <el-input
            v-model="queryParams.studentName"
            :placeholder="t('common.inputTextPlaceholder') + t('studentCourse.list.studentName')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item :label="t('studentCourse.list.studentCode')" prop="studentCode">
          <el-input
            v-model="queryParams.studentCode"
            :placeholder="t('common.inputTextPlaceholder') + t('studentCourse.list.studentCode')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            {{ t('common.search') }}
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            {{ t('common.reset') }}
          </el-button>

          <SendChat2 :list="chatList" v-hasPermi="['course:course:sms']" />
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <vxe-table
        ref="tableRef"
        id="iCourseStudentList-table"
        v-loading="loading"
        :data="list"
        stripe
        :row-config="{ keyField: 'id' }"
        :custom-config="vxeTableConfig"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
        :checkbox-config="{ reserve: true }"
        @checkbox-all="collectSelect"
        @checkbox-change="collectSelect"
      >
        <vxe-column type="checkbox" width="50" align="center" fixed="left" />
        <vxe-column
          :title="t('studentCourse.list.studentName')"
          field="studentName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('studentCourse.list.studentCode')"
          field="studentCode"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('studentCourse.list.mobile')"
          field="mobile"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('studentCourse.list.signNum')"
          field="signNum"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          fixed="right"
          width="100"
        >
          <template #default="scope">
            <el-button link type="primary" @click="handleViewPersonalLog(scope.row)">
              {{ t('studentCourse.list.signLog') }}
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>

  <CourseStudentPersonalSignLog ref="courseStudentPersonalSignLogRef" />
</template>

<script setup lang="ts">
import { CourseStudentListRespVO, StudentApi } from '@/api/course/student'
import type { VxeTableInstance, VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { CourseAdminRespVO } from '@/api/course/course'
import SendChat2 from '@/components/SendChat2/index.vue'
import { AccountSelectVO } from '@/api/system/account'
import CourseStudentPersonalSignLog from '@/views/course/course/components/CourseStudentPersonalSignLog.vue'

/** 课程学生 列表 */
defineOptions({ name: 'CourseStudentList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CourseStudentListRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  courseId: undefined,
  semesterId: undefined,
  studentType: undefined,
  accountId: undefined,
  remark: undefined,
  studentName: undefined,
  studentCode: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.courseId = course.value?.id as any
  loading.value = true
  try {
    const data = await StudentApi.getStudentPageByCourse(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const chatList = ref<AccountSelectVO[]>([])
const tableRef = ref<VxeTableInstance>()
const collectSelect = () => {
  chatList.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ].map((item: CourseStudentListRespVO) => {
    return {
      id: item.accountId,
      studentName: item.studentName,
      studentCode: item.studentCode,
      phone: item.mobile,
      email: item.email
    } as AccountSelectVO
  })
}

const courseStudentPersonalSignLogRef = ref<InstanceType<typeof CourseStudentPersonalSignLog>>()
const handleViewPersonalLog = (row: CourseStudentListRespVO) => {
  courseStudentPersonalSignLogRef.value?.open(row)
}

const course = ref<CourseAdminRespVO>()
const dialogVisible = ref(false)
const open = (row: CourseAdminRespVO) => {
  course.value = row
  dialogVisible.value = true
  getList()
}

defineExpose({ open })
</script>
